package com.zhiyizhen.handheld.component.execute.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.common.StringUtil;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.execute.bean.ExecuteRecordBean;
import com.zhiyizhen.handheld.component.main.bean.ExecTypeBean;
import com.zhiyizhen.handheld.databinding.ItemExecuteRecordBinding;

import java.util.List;

/**
 * 执行记录
 */
public class ExecuteRecordAdapter extends BaseAdapter<ExecuteRecordBean, ExecuteRecordAdapter.ExecuteRecordViewHolder> {

    private List<ExecTypeBean> execTypeBeanList;
    private OnEditClickListener onEditClickListener;

    public void setOnEditClickListener(OnEditClickListener onEditClickListener) {
        this.onEditClickListener = onEditClickListener;
    }

    public ExecuteRecordAdapter(Context context) {
        super(context);
    }

    public void setExecTypeBeanList(List<ExecTypeBean> execTypeBeanList) {
        this.execTypeBeanList = execTypeBeanList;
    }

    @NonNull
    @Override
    public ExecuteRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ExecuteRecordViewHolder(this, ItemExecuteRecordBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class ExecuteRecordViewHolder extends BaseViewHolder<ExecuteRecordBean> {

        private final ItemExecuteRecordBinding binding;

        public ExecuteRecordViewHolder(BaseAdapter<ExecuteRecordBean, ExecuteRecordViewHolder> adapter, ItemExecuteRecordBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(ExecuteRecordBean data, int position) {
            binding.execTypeTv.setText(StringUtil.getExecuteType(execTypeBeanList, data.getExecType()));
            binding.dateTv.setText(data.getExecTime());
            if (TextUtils.equals(data.getExecType(), "输液") || TextUtils.equals(data.getExecType(), "SYZ") || TextUtils.equals(data.getExecType(), "SY")) {
                if (TextUtils.isEmpty(data.getGroupName())) {
                    binding.groupNameTv.setVisibility(View.GONE);
                    binding.divider.setVisibility(View.GONE);
                } else {
                    binding.groupNameTv.setVisibility(View.VISIBLE);
                    binding.divider.setVisibility(View.VISIBLE);
                    binding.groupNameTv.setText(data.getGroupName());
                }
            } else {
                binding.groupNameTv.setVisibility(View.GONE);
                binding.divider.setVisibility(View.GONE);
            }

            binding.timesTv.setText("第" + data.getExecTimes() + "次");
            binding.feedbackTv.setText(data.getExecResult());
            binding.executePersonTv.setText(data.getExecBy());
            binding.executeTimeTv.setText(data.getExecTime());
            binding.editTv.setOnClickListener(v -> {
                if (onEditClickListener != null) {
                    onEditClickListener.onEditClick(data);
                }
            });

            binding.chargeInfoLayout.setVisibility(View.GONE);
            binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
            binding.moreImg.setOnClickListener(v -> {
                if (binding.chargeInfoLayout.getVisibility() == View.VISIBLE) {
                    binding.chargeInfoLayout.setVisibility(View.GONE);
                    binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
                } else {
                    binding.chargeInfoLayout.setVisibility(View.VISIBLE);
                    binding.moreImg.setImageResource(R.drawable.icon_down_arrow);
                }
            });
            binding.dateTv.setOnClickListener(v -> {
                if (binding.chargeInfoLayout.getVisibility() == View.VISIBLE) {
                    binding.chargeInfoLayout.setVisibility(View.GONE);
                    binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
                } else {
                    binding.chargeInfoLayout.setVisibility(View.VISIBLE);
                    binding.moreImg.setImageResource(R.drawable.icon_down_arrow);
                }
            });
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
    }

    public interface OnEditClickListener {
        void onEditClick(ExecuteRecordBean data);
    }
}
